﻿using FarmSmartSleep.Common.Domain;
using FarmSmartSleep.Common.Domain.BeginTable;
using FarmSmartSleep.Common.Domain.BodyWeight;
using FarmSmartSleep.Common.Dtos;
using FarmSmartSleep.Common.Infrastructure;
using MediatR;

namespace FarmSmartSleep.Organ.API.Application.SleepWeightCommands
{
    public class PhaseSearchHandler : IRequestHandler<PhaseSearchCommand, ResultT<PhaseDtos>>
    {
        private readonly IBaseRepository<PhaseDeters> PhaseDetersbaseRepository;
        private readonly IBaseRepository<SleepInfor> sleepinforbaseRepository;
        private readonly IBaseRepository<Variety> varietybaseRepository;
        private readonly IBaseRepository<Cottages> CottagesbaseRepository;
        private readonly IBaseRepository<Fields> FieldsbaseRepository;

        public PhaseSearchHandler(IBaseRepository<PhaseDeters> phaseDetersbaseRepository, IBaseRepository<SleepInfor> sleepinforbaseRepository,
            IBaseRepository<Variety> varietybaseRepository, IBaseRepository<Cottages> CottagesbaseRepository, IBaseRepository<Fields> FieldsbaseRepository)
        {
            PhaseDetersbaseRepository = phaseDetersbaseRepository;
            this.sleepinforbaseRepository = sleepinforbaseRepository;
            this.varietybaseRepository = varietybaseRepository;
            this.FieldsbaseRepository = FieldsbaseRepository;
            this.CottagesbaseRepository = CottagesbaseRepository;
        }
        /// <summary>
        /// 品相测定查询
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<ResultT<PhaseDtos>> Handle(PhaseSearchCommand request, CancellationToken cancellationToken)
        {
            var result= new ResultT<PhaseDtos>() { Code=(int)CodeEnum.search};
            var list=(from a in PhaseDetersbaseRepository.GetAll()
                     join b in sleepinforbaseRepository.GetAll() on a.EarNumber equals b.EarNumber
                     join c in varietybaseRepository.GetAll() on b.VarietyId equals c.VarietyId
                     join d in CottagesbaseRepository.GetAll() on b.RecordcottageId equals d.CottagesId
                     join e in FieldsbaseRepository.GetAll() on b.FieIdId equals e.FieIdId
                     select new PhaseDtos()
                     {
                         PhaseDeterId=a.PhaseDeterId,
                         PhaseDeterDate=a.PhaseDeterDate,
                         EarNumber=a.EarNumber,
                         Tooth=a.Tooth,
                         Hair=a.Hair,
                         DarkSpot=a.DarkSpot,
                         FootColor=a.FootColor,
                         MouthColor=a.MouthColor,
                         Horn=a.Horn,
                         Fat=a.Fat,
                         Gender=b.Gender,
                         VarietyId=b.VarietyId,
                         VarietyName=c.VarietyName,
                         CottageName=d.CottageName,
                         FieIdName=e.FieIdName,
                         CreateId=a.CreateId,
                         CreateTime=a.CreateTime,
                         UpdateId=a.UpdateId,
                         UpdateTime=a.UpdateTime,
                     }).ToList();
            if (!string.IsNullOrEmpty(request.EarNumber))
            {
                list=list.Where(x=>x.EarNumber==request.EarNumber).ToList();
            }
            if (request.VarietyId != 0)
            {
                list=list.Where(x=>x.VarietyId==request.VarietyId).ToList();
            }
            if (request.Gender != 0)
            {
                list=list.Where(x=>x.Gender==request.Gender).ToList();
            }
            result.Data = list;
            return result;
        }
    }
}
